﻿import 'package:azring/gen/assets.gen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'create_plan_logic.dart';

/// 创建计划页面
class CreatePlanView extends GetView<CreatePlanLogic> {
  final String title;
  final String? planId;
  const CreatePlanView({super.key, this.title = '自定义生成计划', this.planId});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF261B0D),
      body: Stack(
        children: [
          // 渐变背景区域
          Container(
            height: 141.h,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.bottomCenter,
                end: Alignment.topCenter,
                colors: [Color(0xFF221A01), Color(0xFF7D3200)],
                stops: [0.0, 1.0],
              ),
            ),
            child: SafeArea(
              child: Column(
                children: [
                  _buildCustomAppBar(),
                  const Spacer(),
                ],
              ),
            ),
          ),
          // 内容区域
          Container(
            margin: EdgeInsets.only(top: 90.h),
            decoration: const BoxDecoration(
              color: Color(0xFF261B0D),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(36),
                topRight: Radius.circular(36),
              ),
            ),
            child: SingleChildScrollView(
              padding: EdgeInsets.all(17.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildPlanNameInput(),
                  SizedBox(height: 32.h),
                  _buildVitalSignsSection(),
                  SizedBox(height: 32.h),
                  _buildDietGoalsSection(),
                  SizedBox(height: 32.h),
                  _buildEventCheckinSection(),
                  SizedBox(height: 32.h),
                  _buildEmotionManagementSection(),
                  SizedBox(height: 32.h),
                  _buildPlanCycleSection(),
                  SizedBox(height: 32.h),
                  _buildActionButtons(),
                  SizedBox(height: 77.h),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCustomAppBar() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
      child: Row(
        children: [
          GestureDetector(
            onTap: () => Get.back(),
            child: Icon(
              Icons.arrow_back_ios,
              color: Colors.white,
              size: 20.sp,
            ),
          ),
          Expanded(
            child: Center(
              child: Text(
                title,
                style: TextStyle(
                  fontSize: 20.sp,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),
            ),
          ),
          Obx(() => controller.isViewMode.value
              ? GestureDetector(
                  onTap: () => controller.toggleEditMode(),
                  child: Icon(
                    controller.isEditMode.value ? Icons.close : Icons.edit,
                    color: Colors.white,
                    size: 20.sp,
                  ),
                )
              : GestureDetector(
                  onTap: () {
                    // TODO: 实现设置功能
                  },
                  child: Icon(
                    Icons.tune,
                    color: Colors.white,
                    size: 20.sp,
                  ),
                )),
        ],
      ),
    );
  }

  Widget _buildPlanNameInput() {
    return Center(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Obx(() => controller.isViewMode.value && !controller.isEditMode.value
              ? Text(
                  controller.planNameDisplay.value.isEmpty
                      ? '计划名称'
                      : controller.planNameDisplay.value,
                  style: TextStyle(
                    fontSize: 18.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w600,
                  ),
                )
              : SizedBox(
                  width: 207.w,
                  child: TextField(
                    controller: controller.planNameController,
                    style: TextStyle(
                      fontSize: 18.sp,
                      color: Colors.white,
                    ),
                    decoration: InputDecoration(
                      isDense: true,
                      hintText: '输入计划名称',
                      hintStyle: TextStyle(
                        fontSize: 18.sp,
                        color: Colors.white.withOpacity(0.3),
                      ),
                      filled: false,
                      border: const UnderlineInputBorder(
                        borderSide: BorderSide(
                          color: Colors.white,
                          width: 1,
                        ),
                      ),
                      enabledBorder: const UnderlineInputBorder(
                        borderSide: BorderSide(
                          color: Colors.white,
                          width: 1,
                        ),
                      ),
                      focusedBorder: const UnderlineInputBorder(
                        borderSide: BorderSide(
                          color: Colors.white,
                          width: 1,
                        ),
                      ),
                      contentPadding: EdgeInsets.symmetric(
                        horizontal: 0,
                        vertical: 6.h,
                      ),
                    ),
                  ),
                )),
        ],
      ),
    );
  }

  Widget _buildVitalSignsSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '生命体征关键目标',
          style: TextStyle(
            fontSize: 18.sp,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
        SizedBox(height: 20.h),
        _buildSectionCard(
          children: [
            Obx(() => _buildVitalSignItem(
              icon: Assets.plan.steps.image(width: 16.w, height: 16.w),
              label: '步数',
              rightWidget: controller.isViewMode.value && !controller.isEditMode.value
                  ? Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text(
                          controller.stepCountDisplay.value.isEmpty
                              ? '-'
                              : controller.stepCountDisplay.value,
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: Colors.white,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        Text(
                          ' 步',
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    )
                  : SizedBox(
                      width: 80.w,
                      child: TextField(
                        controller: controller.stepCountController,
                        keyboardType: TextInputType.number,
                        inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: Colors.white70,
                        ),
                        decoration: InputDecoration(
                          hintText: '输入',
                          hintStyle: TextStyle(
                            fontSize: 14.sp,
                            color: Colors.white70,
                          ),
                          filled: false,
                          border: InputBorder.none,
                          enabledBorder: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          contentPadding: EdgeInsets.zero,
                          suffixText: '步',
                          suffixStyle: TextStyle(
                            fontSize: 14.sp,
                            color: Colors.white70,
                          ),
                          isDense: true,
                        ),
                      ),
                    ),
            )),
            Obx(() => _buildVitalSignItem(
                  icon: null,
                  label: '时长',
                  rightWidget: controller.isViewMode.value && !controller.isEditMode.value
                      ? Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(
                              controller.durationDisplay.value.isEmpty
                                  ? '-'
                                  : controller.durationDisplay.value,
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: Colors.white,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                            Text(
                              ' 分钟',
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: Colors.white,
                              ),
                            ),
                          ],
                        )
                      : SizedBox(
                          width: 80.w,
                          child: TextField(
                            controller: controller.durationController,
                            keyboardType: TextInputType.number,
                            inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                            enabled: controller.hasStepCountInput.value,
                            style: TextStyle(
                              fontSize: 14.sp,
                              color: controller.hasStepCountInput.value
                                  ? Colors.white70
                                  : Colors.white38,
                            ),
                            decoration: InputDecoration(
                              hintText: controller.hasStepCountInput.value
                                  ? '输入'
                                  : '需先输入步数',
                              hintStyle: TextStyle(
                                fontSize: 14.sp,
                                color: controller.hasStepCountInput.value
                                    ? Colors.white70
                                    : Colors.white38,
                              ),
                              filled: false,
                              border: InputBorder.none,
                              enabledBorder: InputBorder.none,
                              focusedBorder: InputBorder.none,
                              contentPadding: EdgeInsets.zero,
                              suffixText: '分钟',
                              suffixStyle: TextStyle(
                                fontSize: 14.sp,
                                color: controller.hasStepCountInput.value
                                    ? Colors.white70
                                    : Colors.white38,
                              ),
                              isDense: true,
                            ),
                          ),
                        ),
                )),
            Obx(() => _buildVitalSignItem(
                  icon: null,
                  label: '运动卡路里（消耗端）',
                  rightWidget: controller.isViewMode.value && !controller.isEditMode.value
                      ? Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(
                              controller.exerciseCalorieDisplay.value.isEmpty
                                  ? '-'
                                  : controller.exerciseCalorieDisplay.value,
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: Colors.white,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                            Text(
                              ' 千卡',
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: Colors.white,
                              ),
                            ),
                          ],
                        )
                      : SizedBox(
                          width: 80.w,
                          child: TextField(
                            controller: controller.exerciseCalorieController,
                            keyboardType: TextInputType.number,
                            inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                            enabled: controller.hasStepCountInput.value,
                            style: TextStyle(
                              fontSize: 14.sp,
                              color: controller.hasStepCountInput.value
                                  ? Colors.white70
                                  : Colors.white38,
                            ),
                            decoration: InputDecoration(
                              hintText: controller.hasStepCountInput.value
                                  ? '输入'
                                  : '需先输入步数',
                              hintStyle: TextStyle(
                                fontSize: 14.sp,
                                color: controller.hasStepCountInput.value
                                    ? Colors.white70
                                    : Colors.white38,
                              ),
                              filled: false,
                              border: InputBorder.none,
                              enabledBorder: InputBorder.none,
                              focusedBorder: InputBorder.none,
                              contentPadding: EdgeInsets.zero,
                              suffixText: '千卡',
                              suffixStyle: TextStyle(
                                fontSize: 14.sp,
                                color: controller.hasStepCountInput.value
                                    ? Colors.white70
                                    : Colors.white38,
                              ),
                              isDense: true,
                            ),
                          ),
                        ),
                )),
            Obx(() => _buildVitalSignItem(
              icon: Assets.plan.sleep.image(width: 16.w, height: 16.w),
              label: '睡眠',
              rightWidget: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    controller.sleepQuality.value,
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.white,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  Text(
                    ' 质量',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.white,
                    ),
                  ),
                ],
              ),
            )),
            _buildVitalSignItem(
              icon: Assets.plan.hr.image(width: 18.w, height: 16.w),
              label: '静息心率',
              rightWidget: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    '50-100',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.white,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  Text(
                    ' BPM',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.white,
                    ),
                  ),
                ],
              ),
            ),
            Obx(() => _buildVitalSignItem(
                  icon: null,
                  label: '平均心率变异性',
                  rightWidget: controller.isLoadingHrv.value
                      ? SizedBox(
                          width: 16.w,
                          height: 16.w,
                          child: const CircularProgressIndicator(
                            strokeWidth: 2,
                            valueColor:
                                AlwaysStoppedAnimation<Color>(Colors.white70),
                          ),
                        )
                      : Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(
                              controller.hrvValue.value,
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: Colors.white,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                            Text(
                              ' HRV',
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: Colors.white,
                              ),
                            ),
                          ],
                        ),
                )),
            _buildVitalSignItem(
              icon: null,
              label: '血氧饱和度',
              rightWidget: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    '98',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.white,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  Text(
                    ' %',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.white,
                    ),
                  ),
                ],
              ),
              isLast: true, // 这是最后一个项目
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildDietGoalsSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '饮食目标',
          style: TextStyle(
            fontSize: 18.sp,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
        SizedBox(height: 20.h),
        _buildSectionCard(
          children: [
            Obx(() => _buildVitalSignItem(
                  icon: Assets.plan.diet.image(width: 12.w, height: 16.w),
                  label: '每日饮食卡路里（摄入端）',
                  rightWidget: controller.isViewMode.value && !controller.isEditMode.value
                      ? Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(
                              controller.dietCalorieDisplay.value.isEmpty
                                  ? '-'
                                  : controller.dietCalorieDisplay.value,
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: Colors.white,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                            Text(
                              ' 千卡',
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: Colors.white,
                              ),
                            ),
                          ],
                        )
                      : SizedBox(
                          width: 80.w,
                          child: TextField(
                            controller: controller.dietCalorieController,
                            keyboardType: TextInputType.number,
                            inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                            enabled: controller.hasStepCountInput.value,
                            style: TextStyle(
                              fontSize: 14.sp,
                              color: controller.hasStepCountInput.value
                                  ? Colors.white70
                                  : Colors.white38,
                            ),
                            decoration: InputDecoration(
                              hintText: controller.hasStepCountInput.value
                                  ? '输入'
                                  : '需先输入步数',
                              hintStyle: TextStyle(
                                fontSize: 14.sp,
                                color: controller.hasStepCountInput.value
                                    ? Colors.white70
                                    : Colors.white38,
                              ),
                              isDense: true,
                              filled: false,
                              border: InputBorder.none,
                              enabledBorder: InputBorder.none,
                              focusedBorder: InputBorder.none,
                              contentPadding: EdgeInsets.zero,
                              suffixText: '千卡',
                              suffixStyle: TextStyle(
                                fontSize: 14.sp,
                                color: controller.hasStepCountInput.value
                                    ? Colors.white70
                                    : Colors.white38,
                              ),
                            ),
                          ),
                        ),
                )),
            Padding(
              padding:
                  EdgeInsets.only(left: 24.w), // 与本卡片标题起始对齐（图标12.w + 间距12.w）
              child: Text(
                '每日按时完成三餐并记录',
                style: TextStyle(
                  fontSize: 12.sp,
                  color: Colors.white.withOpacity(0.7),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildEventCheckinSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '事件打卡',
          style: TextStyle(
            fontSize: 18.sp,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
        SizedBox(height: 20.h),
        _buildSectionCard(
          children: [
            Row(
              children: [
                Assets.plan.flag.image(width: 16.w, height: 16.w),
                SizedBox(width: 8.w),
                Text(
                  '每日推荐打卡事件',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: Colors.white70,
                  ),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            Padding(
              padding: EdgeInsets.only(left: 24.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Obx(() => Wrap(
                    spacing: 12.w,
                    runSpacing: 12.h,
                    children: controller.availableEvents
                        .map((event) => _buildEventTag(
                              event,
                              isSelected:
                                  controller.selectedEvents.contains(event),
                              onTap: controller.isViewMode.value && !controller.isEditMode.value
                                  ? null
                                  : () => controller.toggleEvent(event),
                              onLongPress: controller.isViewMode.value && !controller.isEditMode.value
                                  ? null
                                  : () => controller.deleteEvent(event),
                            ))
                        .toList(),
                  )),
                  SizedBox(height: 12.h),
                  Text(
                    '事件由小A管家根据问卷规划生成',
                    style: TextStyle(
                      fontSize: 12.sp,
                      color: Colors.white.withOpacity(0.6),
                    ),
                  ),
                  // 自定义事件输入框（仅在非查看模式或编辑模式下显示）
                  Obx(() => (controller.isViewMode.value && !controller.isEditMode.value)
                      ? const SizedBox.shrink()
                      : Column(
                          children: [
                            SizedBox(height: 16.h),
                            Row(
                              children: [
                                Expanded(
                                  child: Container(
                                    height: 40.h,
                                    decoration: BoxDecoration(
                                      color: Colors.white.withOpacity(0.05),
                                      borderRadius: BorderRadius.circular(20.r),
                                      border: Border.all(
                                        color: Colors.white.withOpacity(0.2),
                                        width: 1,
                                      ),
                                    ),
                                    child: TextField(
                                      controller: controller.customEventController,
                                      style: TextStyle(
                                        fontSize: 12.sp,
                                        color: Colors.white,
                                      ),
                                      decoration: InputDecoration(
                                        hintText: '输入自定义事件',
                                        hintStyle: TextStyle(
                                          fontSize: 12.sp,
                                          color: Colors.white.withOpacity(0.4),
                                        ),
                                        border: InputBorder.none,
                                        contentPadding: EdgeInsets.symmetric(
                                          horizontal: 16.w,
                                          vertical: 10.h,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                                SizedBox(width: 12.w),
                                GestureDetector(
                                  onTap: () => controller.addCustomEvent(),
                                  child: Container(
                                    width: 40.w,
                                    height: 40.h,
                                    decoration: BoxDecoration(
                                      shape: BoxShape.circle,
                                      color: const Color(0xFFDB885F).withOpacity(0.2),
                                      border: Border.all(
                                        color: const Color(0xFFDB885F),
                                        width: 1,
                                      ),
                                    ),
                                    child: Icon(
                                      Icons.add,
                                      color: const Color(0xFFDB885F),
                                      size: 20.sp,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        )),
                ],
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildEmotionManagementSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '情绪管理',
          style: TextStyle(
            fontSize: 18.sp,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
        SizedBox(height: 20.h),
        _buildSectionCard(
          children: [
            Row(
              children: [
                Container(
                  width: 32.w,
                  height: 32.w,
                  decoration: BoxDecoration(
                    color: Colors.red.withOpacity(0.2),
                    borderRadius: BorderRadius.circular(16.r),
                  ),
                  child: Assets.plan.emotional.image(width: 16.w, height: 16.w),
                ),
                SizedBox(width: 12.w),
                Expanded(
                  child: Text(
                    '每日压力值',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.white,
                    ),
                  ),
                ),
                Obx(() => Text(
                  '${controller.emotionManagement.value} HRV',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w600,
                  ),
                )),
              ],
            ),
            SizedBox(height: 16.h),
            Padding(
              padding: EdgeInsets.only(left: 44.w), // 与标题起始对齐（图标32.w + 间距12.w）
              child: Text(
                '每日保持放松好心情-有益于身心健康',
                style: TextStyle(
                  fontSize: 12.sp,
                  color: Colors.white.withOpacity(0.7),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildPlanCycleSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Text(
              '计划周期',
              style: TextStyle(
                fontSize: 18.sp,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
            const Spacer(),
          ],
        ),
        SizedBox(height: 20.h),
        _buildSectionCard(
          children: [
            Row(
              children: [
                Assets.plan.period.image(width: 18.w, height: 16.w),
                SizedBox(width: 8.w),
                Text(
                  '周期时长',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: Colors.white,
                  ),
                ),
                Spacer(),
                Obx(() => GestureDetector(
                  onTap: controller.isViewMode.value && !controller.isEditMode.value
                      ? null
                      : () => controller.toggleDefaultPlan(),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        '设为默认',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: Colors.white.withOpacity(0.7),
                        ),
                      ),
                      SizedBox(width: 8.w),
                      Container(
                        width: 20.w,
                        height: 20.w,
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: controller.isDefaultPlan.value
                              ? Colors.orange
                              : Colors.grey.withOpacity(0.5),
                        ),
                      ),
                    ],
                  ),
                )),
              ],
            ),
            SizedBox(height: 20.h),
            LayoutBuilder(
              builder: (context, constraints) {
                return Obx(() {
                  final isReadOnly = controller.isViewMode.value && !controller.isEditMode.value;
                  return GestureDetector(
                    onTapDown: isReadOnly
                        ? null
                        : (details) {
                            final tapPosition = details.localPosition.dx;
                            final percentage = tapPosition / constraints.maxWidth;
                            final newCycle = (percentage * 45).round().clamp(15, 45);
                            // 调整到最近的15天倍数
                            final adjustedCycle =
                                ((newCycle / 15).round() * 15).clamp(15, 45);
                            controller.updatePlanCycle(adjustedCycle);
                          },
                    child: Container(
                      width: double.infinity,
                      height: 6.h,
                      decoration: BoxDecoration(
                        color: Colors.white.withOpacity(0.2),
                        borderRadius: BorderRadius.circular(3.r),
                      ),
                      child: Stack(
                        children: [
                          Container(
                            width: constraints.maxWidth *
                                (controller.planCycle.value / 45),
                            height: 6.h,
                            decoration: BoxDecoration(
                              color: Colors.orange,
                              borderRadius: BorderRadius.circular(3.r),
                            ),
                          ),
                          () {
                            final progressWidth = constraints.maxWidth *
                                (controller.planCycle.value / 45);
                            return Positioned(
                              left: (progressWidth - 10.w)
                                  .clamp(0, constraints.maxWidth - 20.w),
                              top: -5.h,
                              child: Container(
                                width: 20.w,
                                height: 20.w,
                                decoration: BoxDecoration(
                                  shape: BoxShape.circle,
                                  color: Colors.orange,
                                ),
                              ),
                            );
                          }(),
                        ],
                      ),
                    ),
                  );
                });
              },
            ),
            SizedBox(height: 8.h),
            Obx(() => Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '开始',
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: Colors.white.withOpacity(0.6),
                      ),
                    ),
                    Text(
                      '15天',
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: controller.planCycle.value == 15
                            ? Colors.orange
                            : Colors.white.withOpacity(0.6),
                        fontWeight: controller.planCycle.value == 15
                            ? FontWeight.w600
                            : FontWeight.normal,
                      ),
                    ),
                    Text(
                      '30天',
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: controller.planCycle.value == 30
                            ? Colors.orange
                            : Colors.white.withOpacity(0.6),
                        fontWeight: controller.planCycle.value == 30
                            ? FontWeight.w600
                            : FontWeight.normal,
                      ),
                    ),
                    Text(
                      '45天',
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: controller.planCycle.value == 45
                            ? Colors.orange
                            : Colors.white.withOpacity(0.6),
                        fontWeight: controller.planCycle.value == 45
                            ? FontWeight.w600
                            : FontWeight.normal,
                      ),
                    ),
                  ],
                )),
            SizedBox(height: 16.h),
            Text(
              '坚持周期越长,获得积分奖励越高~',
              style: TextStyle(
                fontSize: 12.sp,
                color: Colors.white.withOpacity(0.6),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildSectionCard({
    String? title, // 改为可选参数
    required List<Widget> children,
  }) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(20.w),
      decoration: BoxDecoration(
        color: const Color(0xFF282528),
        borderRadius: BorderRadius.circular(16.r),
        boxShadow: [
          BoxShadow(
            color: const Color(0xFF0F172A).withOpacity(0.02),
            offset: const Offset(0, 8),
            blurRadius: 16,
            spreadRadius: 0,
          ),
          BoxShadow(
            color: const Color(0xFF0F172A).withOpacity(0.03),
            offset: const Offset(0, 4),
            blurRadius: 8,
            spreadRadius: 0,
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (title != null) ...[
            Text(
              title,
              style: TextStyle(
                fontSize: 18.sp,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
            SizedBox(height: 20.h),
          ],
          ...children,
        ],
      ),
    );
  }

  Widget _buildVitalSignItem({
    Image? icon,
    required String label,
    required Widget rightWidget,
    bool isLast = false, // 添加isLast参数
    Color? labelColor, // 添加labelColor参数
    bool alignToTitle = false, // 当无图标时是否与文字起始对齐
    double? alignOffset, // 无图标时用于与文字起始对齐的占位宽度
  }) {
    return Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(vertical: 19.h),
          child: Row(
            children: [
              if (icon != null) ...[
                icon,
                SizedBox(width: 12.w),
              ] else if (!alignToTitle) ...[
                SizedBox(width: 16.w, height: 16.w),
                SizedBox(width: 12.w),
              ] else ...[
                SizedBox(width: (alignOffset ?? 16.w), height: 16.w),
                SizedBox(width: 12.w),
              ],
              Expanded(
                child: Text(
                  label,
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: labelColor ?? Colors.white, // 使用传入的labelColor或默认白色
                  ),
                ),
              ),
              rightWidget,
            ],
          ),
        ),
        if (!isLast) // 只有不是最后一个项目时才显示分割线
          Container(
            height: 1.h,
            color: const Color(0xFF402B1E),
          ),
      ],
    );
  }

  Widget _buildEventTag(String text,
      {bool isSelected = false, VoidCallback? onTap, VoidCallback? onLongPress}) {
    return GestureDetector(
      onTap: onTap,
      onLongPress: onLongPress,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
        decoration: BoxDecoration(
          color: isSelected
              ? const Color(0xFFDB885F).withOpacity(0.2)
              : Colors.white.withOpacity(0.1),
          borderRadius: BorderRadius.circular(20.r),
          border: Border.all(
            color: isSelected
                ? const Color(0xFFDB885F)
                : Colors.white.withOpacity(0.2),
            width: 1,
          ),
        ),
        child: Text(
          text,
          style: TextStyle(
            fontSize: 12.sp,
            color: isSelected ? const Color(0xFFDB885F) : Colors.white70,
          ),
        ),
      ),
    );
  }

  Widget _buildActionButtons() {
    return Obx(() {
      // 查看模式且未进入编辑状态时，不显示按钮
      if (controller.isViewMode.value && !controller.isEditMode.value) {
        return const SizedBox.shrink();
      }

      // 新建模式和编辑模式都显示相同的两个按钮
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Container(
            width: 160.w,
            height: 56.h,
            decoration: BoxDecoration(
              gradient: const LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Color(0xFFDB885F),
                  Color(0xFFFFFBCA),
                ],
              ),
              borderRadius: BorderRadius.circular(64.r),
            ),
            child: ElevatedButton(
              onPressed: controller.isLoading.value
                  ? null
                  : () => controller.saveAndEnablePlan(),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.transparent,
                shadowColor: Colors.transparent,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16.r),
                ),
                padding: EdgeInsets.symmetric(vertical: 16.h),
              ),
              child: controller.isLoading.value
                  ? SizedBox(
                      width: 20.w,
                      height: 20.w,
                      child: const CircularProgressIndicator(
                        strokeWidth: 2,
                        valueColor: AlwaysStoppedAnimation<Color>(
                          Color(0xFF2D3748),
                        ),
                      ),
                    )
                  : Text(
                      '启用计划',
                      style: TextStyle(
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w600,
                        color: const Color(0xFF2D3748),
                      ),
                    ),
            ),
          ),
          SizedBox(width: 13.w),
          Container(
            width: 160.w,
            height: 56.h,
            decoration: BoxDecoration(
              gradient: const LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Color(0xFFF9F8F7),
                  Color(0xFFE9DED6),
                ],
              ),
              borderRadius: BorderRadius.circular(64.r),
              boxShadow: [
                BoxShadow(
                  color: const Color(0xFFFDF8F5).withOpacity(0.54),
                  offset: const Offset(0, 0),
                  blurRadius: 11.2,
                  spreadRadius: 0,
                ),
              ],
            ),
            child: ElevatedButton(
              onPressed: controller.isLoading.value
                  ? null
                  : () => controller.saveAndGeneratePlan(),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.transparent,
                shadowColor: Colors.transparent,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16.r),
                ),
                padding: EdgeInsets.symmetric(vertical: 16.h),
              ),
              child: controller.isLoading.value
                  ? SizedBox(
                      width: 20.w,
                      height: 20.w,
                      child: const CircularProgressIndicator(
                        strokeWidth: 2,
                        valueColor: AlwaysStoppedAnimation<Color>(
                          Color(0xFF2D3748),
                        ),
                      ),
                    )
                  : Text(
                      '保存计划',
                      style: TextStyle(
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w600,
                        color: const Color(0xFF2D3748),
                      ),
                    ),
            ),
          ),
        ],
      );
    });
  }
}
